package com.cadre.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cadre.common.utils.common.GenerateUUID;
import com.cadre.common.utils.oldutil.ExcelImpotUtils;
import com.cadre.common.utils.oldutil.PageUtils;
import com.cadre.common.utils.oldutil.Query;
import com.cadre.system.domain.officer.A01;
import com.cadre.system.domain.officer.B01;
import com.cadre.system.domain.officer.PX_RepotDTO;
import com.cadre.system.domain.officer.PxRypxxxEntity;
import com.cadre.system.mapper.B01Mapper;
import com.cadre.system.mapper.PxRypxxxMapper;
import com.cadre.system.service.PxRypxxxService;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;

@Service("pxRypxxxService")
public class PxRypxxxServiceImpl extends ServiceImpl<PxRypxxxMapper, PxRypxxxEntity> implements PxRypxxxService {
    @Autowired
    PxRypxxxMapper pmp;
    @Autowired
    B01Mapper bmp;

    @Override // com.tdqy.cadre.modular.profess.service.PxRypxxxService
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<PxRypxxxEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(PxRypxxxEntity::getCreattime);
        return new PageUtils(this.baseMapper.selectPage(new Query(params).getPage(), lambdaQueryWrapper));
    }

    @Override
    public PageUtils getListForPage(Map<String, Object> map) {
        map.put("offset",(Integer.parseInt(map.get("page").toString())-1)*
                Integer.parseInt(map.get("limit").toString()));
        Page<PX_RepotDTO> page = new Query(map).getPage();
        List<PX_RepotDTO> list = pmp.getListForPage(map);
        page.setRecords(list);
        int total = pmp.getListForPageCount(map);
        page.setTotal(total);
        page.setCurrent(Integer.parseInt(map.get("page").toString()));
        page.setSize(Integer.parseInt(map.get("limit").toString()));
        return new PageUtils(page);
    }

    @Override // com.tdqy.cadre.modular.profess.service.PxRypxxxService
    @Transactional
    public List<PxRypxxxEntity_c> readExcel(MultipartFile file, String unitId) {
        new HashMap();
        Pattern pattern = Pattern.compile("[^0-9]");
        List<PxRypxxxEntity_c> listError = new ArrayList<>();
        List<PxRypxxxEntity_c> listAll = new ArrayList<>();
        String b0101 = null;
        if (unitId != null && !unitId.trim().equals("")) {
            b0101 = ((B01) this.bmp.selectById(unitId)).getB0101();
        }
        try {
            List<Map<Integer, String>> list = ExcelImpotUtils.readExcel(file);
            A01 a = null;
            String name = "";
            Long pxnd = null;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).get(0) != null && list.get(i).get(0).replace(" ", "").replace(" ", "").equals("姓名")) {
                    a = getA01(list.get(i).get(1).replace(" ", "").replace(" ", ""), unitId);
                    name = list.get(i).get(1).replace(" ", "").replace(" ", "");
                } else if ((list.get(i).get(0) == null || !list.get(i).get(0).replace(" ", "").replace(" ", "").equals("年度")) && ((list.get(i).get(1) == null || !list.get(i).get(1).replace(" ", "").replace(" ", "").equals("年度学时数合计")) && a != null && list.get(i).get(1) != null && !list.get(i).get(1).replace(" ", "").replace(" ", "").equals(""))) {
                    if (!list.get(i).get(0).trim().equals("")) {
                        pxnd = Long.valueOf(pattern.matcher(list.get(i).get(0).trim()).replaceAll("").trim());
                    }
                    if (a.getFlowid() == null) {
                        PxRypxxxEntity_c pError = getPxRypxxxEntity(list.get(i));
                        if (b0101 != null) {
                            pError.setName(name + "（" + b0101 + "查无此人）");
                        } else {
                            pError.setName(name + "（查无此人）");
                        }
                        pError.setFlag("1");
                        listError.add(pError);
                    } else if (pxnd != null) {
                        PxRypxxxEntity_c p = getPxRypxxxEntity(list.get(i));
                        p.setPersonid(a.getFlowid());
                        p.setFlowid(GenerateUUID.getUUID());
                        p.setCreattime(new Date());
                        p.setPxnd(pxnd);
                        if (!(p.getHoldunit() == null || p.getPxjg() == null || p.getPxzt() == null || p.getKsrq() == null || p.getJzrq() == null || p.getXz() == null || p.getXs() == null)) {
                            LambdaQueryWrapper<PxRypxxxEntity> lambdaQueryWrapper = new LambdaQueryWrapper();
                            lambdaQueryWrapper.eq(PxRypxxxEntity::getPxzt, p.getPxzt());
                            lambdaQueryWrapper.eq(PxRypxxxEntity::getPersonid, p.getPersonid());
                            if (this.pmp.selectList(lambdaQueryWrapper).size() == 0) {
                                p.setFlag("0");
                                p.setName(name);
                                listAll.add(p);
                            } else {
                                p.setName(name + "（数据重复）");
                                p.setFlag("1");
                                listError.add(p);
                            }
                        }
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        listError.addAll(listAll);
        return listError;
    }

    private PxRypxxxEntity_c getPxRypxxxEntity(Map<Integer, String> map) {
        PxRypxxxEntity_c p = new PxRypxxxEntity_c();
        if (map.get(1) != null) {
            p.setHoldunit(map.get(1));
        }
        if (map.get(2) != null) {
            p.setPxjg(map.get(2));
        }
        if (map.get(3) != null) {
            p.setPxzt(map.get(3));
        }
        if (!(map.get(4) == null || getDate(map.get(4)) == null)) {
            p.setKsrq(getDate(map.get(4)));
        }
        if (!(map.get(5) == null || getDate(map.get(5)) == null)) {
            p.setJzrq(getDate(map.get(5)));
        }
        if (map.get(6) != null && NumberUtils.isCreatable(map.get(6))) {
            p.setXz(Double.valueOf(map.get(6)));
        }
        if (map.get(7) != null && NumberUtils.isCreatable(map.get(7))) {
            p.setXs(Double.valueOf(map.get(7)));
        }
        return p;
    }

    private Date getDate(String str) {
        SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
        if (str.contains("/")) {
            str = dateStr(str.trim().split("\\/"));
        }
        if (str.contains("-")) {
            str = dateStr(str.trim().split("\\-"));
        }
        if (str.contains("-")) {
            str = dateStr(str.trim().split("\\-"));
        }
        if (str.contains(".")) {
            str = dateStr(str.trim().split("\\."));
        }
        try {
            return formater.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    private String dateStr(String[] strArr) {
        if (strArr[1] != null && strArr[1].length() == 1) {
            strArr[1] = "0" + strArr[1];
        }
        if (strArr[2] != null && strArr[2].length() == 1) {
            strArr[2] = "0" + strArr[2];
        }
        return strArr[0] + strArr[1] + strArr[2];
    }

    private A01 getA01(String name, String unitId) {
        A01 a = new A01();
        List<A01> listA01 = this.pmp.getPersonId(name, unitId);
        if (listA01.size() > 0) {
            a = listA01.get(0);
        }
        return a;
    }

    @Override // com.tdqy.cadre.modular.profess.service.PxRypxxxService
    public String getRole(String userId, String str) {
        if (this.pmp.getRole(userId, str).size() >= 1) {
            return "true";
        }
        return "false";
    }

    /* loaded from: PxRypxxxServiceImpl$PxRypxxxEntity_c.class */
    public class PxRypxxxEntity_c extends PxRypxxxEntity {
        private String flag;

        public PxRypxxxEntity_c() {
        }

        public String getFlag() {
            return this.flag;
        }

        public void setFlag(String flag) {
            this.flag = flag;
        }
    }

    @Override // com.tdqy.cadre.modular.profess.service.PxRypxxxService
    public List<PxRypxxxEntity> selectByPxb(String personid) {
        return this.pmp.selectByPxb(personid);
    }

    @Override // com.tdqy.cadre.modular.profess.service.PxRypxxxService
    public List<PxRypxxxEntity> selectListByMap(Map<String, Object> pMap) {
        return this.pmp.selectListByPersonid(pMap);
    }
}
